package com.zufe.khw;

import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数：");
        String str = sc.nextLine();
        int num = Integer.parseInt(str);
        String binaryStr = toBinaryString(num);
        System.out.println("二进制表示为：" + binaryStr);
        System.out.println("二进制表示为：" + Integer.toBinaryString(num));
    }

    private static String toBinaryString(int num) {
        int remainder;
        StringBuilder sb = new StringBuilder();
        while(num > 0){
            remainder = num % 2;
//            sb.insert(0, remainder);
            sb.append(remainder);
            num = num / 2;
        }
        sb.reverse();
        return sb.toString();
    }
}
